home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Utilities / Communications / Pancake Dev Package 1.0 / Editor / Editor.p < prev    next >
Encoding:
Text File  |  1995-09-06  |  11.0 KB  |  469 lines  |  [TEXT/PJMM]

  1. unit Editor;
  2. interface
  3.     uses
  4.         PancakeCommon;
  5.  
  6.     procedure MAIN (params: editParamsPtr);
  7.  
  8. implementation
  9.  
  10.     procedure MAIN (params: editParamsPtr);
  11.         var
  12.             curTextHand: ptr2hand;
  13.             keyBuf: ptr2str;
  14.  
  15.         procedure Write (str: string);
  16.         begin
  17.             WriteProc(str, params^.procs[1]);
  18.         end;
  19.         procedure Writeln (str: string);
  20.         begin
  21.             WritelnProc(str, params^.procs[2]);
  22.         end;
  23.         procedure WriteHand (hand: Handle);
  24.         begin
  25.             WriteHandProc(hand, params^.procs[3]);
  26.         end;
  27.         procedure ListResFile (name: string);
  28.         begin
  29.             ListResFileProc(name, params^.procs[4]);
  30.         end;
  31.         procedure SetNodeAction (action: NodeActionType);
  32.         begin
  33.             SetNodeActionProc(action, params^.procs[5]);
  34.         end;
  35.         procedure RestoreNodeAction;
  36.         begin
  37.             RestoreNodeActionProc(params^.procs[6]);
  38.         end;
  39.         procedure JumpTo (x, y: integer);
  40.         begin
  41.             JumpToProc(x, y, params^.procs[7]);
  42.         end;
  43.         procedure Out (str: string);
  44.         begin
  45.             OutProc(str, params^.procs[8]);
  46.         end;
  47.         procedure OutPtr (buf: ptr; size: longint);
  48.         begin
  49.             OutPtrProc(buf, size, params^.procs[9]);
  50.         end;
  51.         procedure Report (where: integer; str: string);
  52.         begin
  53.             ReportProc(where, str, params^.procs[10]);
  54.         end;
  55.         function GetVarPtr (which: integer): ptr;
  56.         begin
  57.             GetVarPtr := GetVarPtrProc(which, params^.procs[11]);
  58.         end;
  59.         procedure ListHand (hand: handle);
  60.         begin
  61.             ListHandProc(hand, params^.procs[12]);
  62.         end;
  63.         function ListTextFile (pathname, filename: string): OSErr;
  64.         begin
  65.             ListTextFile := ListTextFileProc(pathname, filename, params^.procs[13]);
  66.         end;
  67.         procedure LettersPrompt (prompt, possible: string; len: byte);
  68.         begin
  69.             LettersPromptProc(prompt, possible, len, params^.procs[14]);
  70.         end;
  71.         procedure PasswordPrompt (prompt, possible: string; len: byte);
  72.         begin
  73.             PasswordPromptProc(prompt, possible, len, params^.procs[15]);
  74.         end;
  75.         procedure NumbersPrompt (prompt, possible: string; max: longint);
  76.         begin
  77.             NumbersPromptProc(prompt, possible, max, params^.procs[16]);
  78.         end;
  79.         procedure AutoPrompt (prompt, possible: string);
  80.         begin
  81.             AutoPromptProc(prompt, possible, params^.procs[17]);
  82.         end;
  83.         procedure YesNoPrompt (prompt: string; yesDefault: boolean);
  84.         begin
  85.             YesNoPromptProc(prompt, yesDefault, params^.procs[18]);
  86.         end;
  87.         procedure DatePrompt (prompt: string);
  88.         begin
  89.             DatePromptProc(prompt, params^.procs[19]);
  90.         end;
  91.         procedure PhonePrompt (prompt: string);
  92.         begin
  93.             PhonePromptProc(prompt, params^.procs[20]);
  94.         end;
  95.         procedure ClrScr;
  96.         begin
  97.             ClrScrProc(params^.procs[21]);
  98.         end;
  99.         function ReplacePercents (str: string; replaceProc: procPtr; user: userRecPtr): string;
  100.         begin
  101.             ReplacePercents := ReplacePercentsProc(str, replaceProc, user, params^.procs[22]);
  102.         end;
  103.         function HasAccess (acs: string): boolean;
  104.         begin
  105.             HasAccess := HasAccessProc(acs, params^.procs[23]);
  106.         end;
  107.         procedure SendFile (protocol: char; path, filename: string);
  108.         begin
  109.             SendFileProc(protocol, path, filename, params^.procs[24]);
  110.         end;
  111.         procedure ReceiveFile (protocol: char; path, filename: string; theRout: ProcPtr);
  112.         begin
  113.             ReceiveFileProc(protocol, path, filename, params^.procs[25]);
  114.         end;
  115.  
  116.         function s2i (t1: str255): longint;
  117.             var
  118.                 t2: longint;
  119.         begin
  120.             StringToNum(t1, t2);
  121.             s2i := t2
  122.         end;
  123.         function i2s (t1: longint): string;
  124.             var
  125.                 t2: str255;
  126.         begin
  127.             NumToString(t1, t2);
  128.             i2s := t2
  129.         end;
  130.  
  131.         function GetStr (which: integer): string;
  132.             var
  133.                 str: str255;
  134.         begin
  135.             UseResFile(params^.externRefnum);
  136.             GetIndString(str, 128, which);
  137.             UseResFile(params^.appRefnum);
  138.             GetStr := str;
  139.         end;
  140.  
  141.         procedure InitStorage;
  142.             var
  143.                 err: OSErr;
  144.         begin
  145.             curTextHand := ptr2hand(GetVarPtr(vCurTextHand));
  146.             with params^, privates^^ do
  147.                 if curTextHand^ = nil then
  148.                     begin
  149.                         EditText := EditHand(NewHandle(1024));
  150.                         EditSize := 1024;
  151.                         EditPos := 0;
  152.                     end
  153.                 else
  154.                     begin
  155.                         EditText := EditHand(curTextHand^);
  156.                         err := HandToHand(handle(EditText));
  157.                         if err <> noErr then
  158.                             Writeln('Memory Full!!! Fatal Error');
  159.                         EditPos := GetHandleSize(handle(EditText));
  160.                         EditSize := EditPos + 1024;
  161.                         SetHandleSize(handle(EditText), EditSize);
  162.                         EditSize := GetHandleSize(handle(EditText));
  163.                         ListHand(curTextHand^);
  164.                     end;
  165.         end;
  166.  
  167.         procedure SaveText;
  168.             var
  169.                 err: OSErr;
  170.         begin
  171.             with params^ do
  172.                 begin
  173.                     SetHandleSize(handle(privates^^.EditText), privates^^.EditPos - length(privates^^.curLine));
  174.                     curTextHand^ := handle(privates^^.EditText);
  175.                     HLockHi(curTextHand^);
  176.                     DisposeHandle(handle(privates));
  177.                     privates := nil;
  178.                     RestoreNodeAction;
  179.                     Exit(Main);
  180.                 end;
  181.         end;
  182.  
  183.         procedure Abort;
  184.             var
  185.                 err: OSErr;
  186.         begin
  187.             with params^ do
  188.                 begin
  189.                     DisposeHandle(Handle(Privates^^.EditText));
  190.                     curTextHand^ := nil;
  191.                     DisposeHandle(handle(privates));
  192.                     privates := nil;
  193.                     RestoreNodeAction;
  194.                     Exit(Main);
  195.                 end;
  196.         end;
  197.  
  198.         function UpperCase (str: string): string;
  199.             var
  200.                 i, j: byte;
  201.         begin
  202.             for i := 1 to length(str) do
  203.                 begin
  204.                     j := ord(str[i]);
  205.                     if (j > 96) and (j < 123) then
  206.                         str[i] := chr(j - 32);
  207.                 end;
  208.             UpperCase := str;
  209.         end;
  210.  
  211.         procedure ProccessLine;
  212.         begin
  213.             with params^.privates^^ do
  214.                 begin
  215.                     curLine := UpperCase(curLine);
  216.                     if (curLine = '/S') or (curLine = '/ES') or (curLine = '/ESN') or (curLine = '/ESP') then
  217.                         SaveText
  218.                     else if curLine = '/ABT' then
  219.                         Abort
  220.                     else if (curLine = '/?') or (curLine = '/H') or (curLine = '/HELP') then
  221.                         begin
  222.                             EditPos := EditPos - length(curLine);
  223.                             ListResFile('Editor Help');
  224.                         end;
  225.                     curLine := '';
  226.                 end
  227.         end;
  228.  
  229.         function FillChar (chh: char; len: byte): string;
  230.             var
  231.                 b: byte;
  232.                 tstr: string;
  233.         begin
  234.             tstr := '';
  235.             for b := 1 to len do
  236.                 tstr[b] := chh;
  237.             tstr[0] := chr(len);
  238.             FillChar := tstr;
  239.         end;
  240.  
  241.         procedure WordWrap;
  242.             var
  243.                 num: byte;
  244.         begin
  245.             with params^.privates^^ do
  246.                 begin
  247.                     num := EditPos + 1;
  248.                     repeat
  249.                         num := num - 1;
  250.                     until (num = 1) | (EditText^^[num] = ' ') | (EditText^^[num] = Return);
  251.                     if (EditText^^[num] = ' ') then
  252.                         begin
  253.                             CurLine := '';
  254.                             BlockMove(@EditText^^[num + 1], @CurLine[1], EditPos - num);
  255.                             CurLine[0] := chr(EditPos - num);
  256.                             EditText^^[num] := Return;
  257.                             Write(concat(FillChar(Backspace, EditPos - num), FillChar(' ', EditPos - num), FillChar(Backspace, EditPos - num)));
  258.                             Write(return);
  259.                             Write(CurLine);
  260.                         end
  261.                     else
  262.                         begin
  263.                             CurLine := '';
  264.                             EditPos := EditPos + 1;
  265.                             EditText^^[EditPos] := Return;
  266.                             Write(return);
  267.                         end;
  268.                 end;
  269.         end;
  270.  
  271.         procedure Edit;
  272.         begin
  273.             with params^.privates^^ do
  274.                 begin
  275.                     SetHandleSize(handle(editText), editPos);
  276.                     curTextHand^ := handle(editText);
  277.                     curLine := '';
  278.                     stage := Editing;
  279.                     ClrScr;
  280.                     Write(GetStr(1));
  281.                     InitStorage;
  282.                 end;
  283.         end;
  284.  
  285.         procedure IdleExt;
  286.             var
  287.                 curAnswer: ptr2str;
  288.         begin
  289.             curTextHand := ptr2hand(GetVarPtr(vCurTextHand));
  290.             curAnswer := ptr2str(GetVarPtr(vCurAnswer));
  291.             with params^.privates^^ do
  292.                 begin
  293.                     if prompt = SureAbort then
  294.                         begin
  295.                             if curAnswer^ = 'Yes' then
  296.                                 Abort
  297.                             else
  298.                                 Edit;
  299.                         end
  300.                     else
  301.                         case curAnswer^[1] of
  302.                             'A': 
  303.                                 begin
  304.                                     Writeln('bort');
  305.                                     Abort;
  306.                                 end;
  307.                             'S': 
  308.                                 begin
  309.                                     Writeln('ave');
  310.                                     SaveText;
  311.                                 end;
  312.                             Return: 
  313.                                 begin
  314.                                     Writeln('Save');
  315.                                     SaveText;
  316.                                 end;
  317.                             'E': 
  318.                                 begin
  319.                                     Writeln('dit');
  320.                                     Edit
  321.                                 end;
  322.                         end;
  323.                 end;
  324.         end;
  325.  
  326.         procedure ProcessKey;
  327.             var
  328.                 ch: char;
  329.                 i, j: integer;
  330.         begin
  331.             curTextHand := ptr2hand(GetVarPtr(vCurTextHand));
  332.             keyBuf := ptr2str(GetVarPtr(vKeyBuf));
  333.             with params^.privates^^ do
  334.                 while (keyBuf^ <> '') do
  335.                     begin
  336.                         ch := keyBuf^[1];
  337.             {$R-}
  338.                         keyBuf^[0] := chr(ord(keyBuf^[0]) - 1);
  339.                         BlockMove(ptr(ord4(keyBuf) + 2), ptr(ord4(keyBuf) + 1), ord(keyBuf^[0]));
  340.             {$R+}
  341.                         if ch = Destructive then
  342.                             ch := Backspace;
  343.                         if (ch = chr(24)) then
  344.                             begin
  345.                                 if curLine <> '' then
  346.                                     begin
  347.                                         EditPos := EditPos + 1;
  348.                                         EditText^^[EditPos] := Return;
  349.                                         Write(Return);
  350.                                     end;
  351.                                 curLine := '';
  352.                                 ptr2bool(GetVarPtr(vNoReturn))^ := true;
  353.                                 AutoPrompt(GetStr(2), concat('SAE', Return));
  354.                                 prompt := SaveEditEtc;
  355.                                 stage := EndPrompting;
  356.                             end
  357.                         else if (ch = chr(3)) then
  358.                             begin
  359.                                 if curLine <> '' then
  360.                                     Write(Return);
  361.                                 YesNoPrompt(GetStr(3), true);
  362.                                 prompt := SureAbort;
  363.                                 stage := EndPrompting;
  364.                             end
  365.                         else if (ch = Escape) then
  366.                         else if (ch = Backspace) then
  367.                             begin
  368.                                 if EditPos > 0 then
  369.                                     begin
  370.                                         if EditText^^[EditPos] = Return then
  371.                                             begin
  372.                                                 EditPos := EditPos - 1;
  373.                                                 j := 1;
  374.                                                 for i := EditPos downto 1 do
  375.                                                     if EditText^^[i] = Return then
  376.                                                         begin
  377.                                                             j := i + 1;
  378.                                                             leave;
  379.                                                         end;
  380.                                                 BlockMove(@EditText^^[j], @curLine[1], EditPos - j + 1);
  381.                     {$R-}
  382.                                                 curLine[0] := chr(EditPos - j + 1);
  383.                     {$R+}
  384.                                                 Out(concat(Escape, '[A'));
  385.                                                 if curLine <> '' then
  386.                                                     Out(concat(Escape, '[', i2s(length(curLine)), 'C'));
  387.                                             end
  388.                                         else
  389.                                             begin
  390.                                                 EditPos := EditPos - 1;
  391.                                                 Write(concat(Backspace, ' ', Backspace));
  392.                                                 Delete(curLine, length(curLine), 1);
  393.                                             end;
  394.                                     end;
  395.                             end
  396.                         else
  397.                             begin
  398.                                 if (ch = Return) then
  399.                                     ProccessLine
  400.                                 else
  401.                                     begin
  402.                                         if ord(curLine[0]) > 77 then
  403.                                             WordWrap;
  404.                                         curLine := concat(curLine, ch);
  405.                                     end;
  406.                                 write(ch);
  407.                                 EditPos := EditPos + 1;
  408.                                 EditText^^[EditPos] := ch;
  409.                                 if EditPos = EditSize then
  410.                                     begin
  411.                                         EditSize := EditSize * 2;
  412.                                         SetHandleSize(handle(EditText), EditSize);
  413.                                         if MemError = memFullErr then
  414.                                             begin
  415.                                                 Writeln('Memory Full!!! Fatal Error');
  416.                                             end;
  417.                                         EditSize := GetHandleSize(handle(EditText));
  418.                                     end;
  419.                             end;
  420.                     end;
  421.         end;
  422.  
  423.         procedure InitEditor;
  424.         begin
  425.             with params^ do
  426.                 begin
  427.                     privates := privatesHand(NewHandle(SizeOf(privatesRec)));
  428.                     HLockHi(handle(privates));
  429.                     with privates^^ do
  430.                         begin
  431.                             Write(GetStr(1));
  432.                             InitStorage;
  433.                             stage := Editing;
  434.                             curLine := '';
  435.                         end;
  436.                 end;
  437.         end;
  438.  
  439.         procedure DisposeEditor;
  440.         begin
  441.             curTextHand := ptr2hand(GetVarPtr(vCurTextHand));
  442.             with params^ do
  443.                 if privates <> nil then
  444.                     begin
  445.                         DisposeHandle(Handle(Privates^^.EditText));
  446.                         curTextHand^ := nil;
  447.                         DisposeHandle(handle(privates));
  448.                         privates := nil;
  449.                         RestoreNodeAction;
  450.                     end;
  451.         end;
  452.  
  453.     begin
  454.         if (params^.message <> DisposeGlobal) and (params^.privates = nil) then
  455.             InitEditor;
  456.         case params^.message of
  457.             Idle: 
  458.                 if params^.privates^^.stage = EndPrompting then
  459.                     IdleExt;
  460.             KeyPressed: 
  461.                 if params^.privates^^.stage = Editing then
  462.                     ProcessKey;
  463.             DisposeExt: 
  464.                 DisposeEditor;
  465.             DisposeGlobal: 
  466.                 params^.globals := nil;
  467.         end;
  468.     end;
  469. end.